package com.ufo.store.management.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.entity.Isszmbalv3BranchInfo;

import java.util.List;

public class Isszmbalv3BranchInfoRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context = null;
    private List<Isszmbalv3BranchInfo> mIsszmbalv3BranchInfoList = null;
    private String mCurrentSelectedBranchNo = "";
    private OnClickListener mListener = null;

    public Isszmbalv3BranchInfoRecyclerViewAdapter(Context context) {
        this.context = context;
    }

    public void setBranchInfoList(List<Isszmbalv3BranchInfo> list){
        this.mIsszmbalv3BranchInfoList = list;
    }

    public void setCurrentSelectedBranchNo(String branchNo){
        mCurrentSelectedBranchNo = branchNo;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        return mIsszmbalv3BranchInfoList.size();
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        TextView noTv;
        TextView nameIv;
        RelativeLayout bg_rl;
        public RecyclerViewHolder(View view) {
            super(view);
            noTv = (TextView) view.findViewById(R.id.no_tv);
            nameIv = (TextView) view.findViewById(R.id.name_tv);
            bg_rl = view.findViewById(R.id.bg_rl);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == 1) {
            holder = new RecyclerViewHolder(LayoutInflater.from(
                    this.context).inflate(R.layout.isszmbalv3_branchinfo_recycleritem, parent,
                    false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final Isszmbalv3BranchInfo info = mIsszmbalv3BranchInfoList.get(position);
        if (info != null) {
            if (holder instanceof RecyclerViewHolder) {
                RecyclerViewHolder holder1 = (RecyclerViewHolder) holder;
                holder1.noTv.setText(info.no.trim());
                holder1.nameIv.setText(info.name.trim());
                if( mCurrentSelectedBranchNo.equals(info.no)){
                    holder1.bg_rl.setBackgroundColor(context.getResources().getColor(R.color.app_key_color));
                    holder1.noTv.setTextColor(Color.parseColor("#ffffff"));
                    holder1.nameIv.setTextColor(Color.parseColor("#ffffff"));
                }else{
                    holder1.bg_rl.setBackgroundColor(Color.TRANSPARENT);
                    holder1.noTv.setTextColor(Color.parseColor("#3c3c3c"));
                    holder1.nameIv.setTextColor(Color.parseColor("#3c3c3c"));
                }
            }
        }
        //点击事件
        if (mListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建两个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemClick(position, info.no, info.name);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemLongClick(pos);
                        return true;
                    }
                });
            }
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position, List payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        }
    }

    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    public interface OnClickListener {
        void onItemClick(int index, String no, String name);

        void onItemLongClick( int position);
    }
}
